package com.example.mediaplayerdemo.broadcast.activity;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.NotificationCompat;

import com.example.mediaplayerdemo.R;
import com.example.mediaplayerdemo.view.MyMarkerView;

public class BroadcastActivity extends AppCompatActivity {
    DynamicReceiver dynamicReceiver;
    private Button button9;
    NotificationManager notificationManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_broadcast);
        IntentFilter filter = new IntentFilter();
        filter.addAction("test");
        notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        dynamicReceiver = new DynamicReceiver();
        registerReceiver(dynamicReceiver, filter);
        initView();
        button9.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.setAction("test");
                intent.putExtra("one", "hello world");
                sendBroadcast(intent);
            }
        });
    }

    @Override
    protected void onPause() {
        super.onPause();
//        unregisterReceiver(new DynamicReceiver());
    }

    private void initView() {
        button9 = (Button) findViewById(R.id.button9);
    }


    private class DynamicReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
//            Toast t = Toast.makeText(context, "动态广播：" + intent.getStringExtra("one"), Toast.LENGTH_SHORT);
//            t.setGravity(Gravity.TOP, 0, 0);//方便录屏，将土司设置在屏幕顶端
//            t.show();
            Notification notification = new Notification();
            Intent resultIntent = new Intent(BroadcastActivity.this, Main2Activity.class);   //点击通知后进入的活动
            //这两句非常重要，使之前的活动不出栈
            resultIntent.setAction(Intent.ACTION_MAIN);
            resultIntent.addCategory(Intent.CATEGORY_LAUNCHER);

            PendingIntent resultPendingIntent = PendingIntent.getActivity(BroadcastActivity.this, 0, resultIntent,
                    PendingIntent.FLAG_UPDATE_CURRENT);//允许更新
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                NotificationChannel mChannel = new NotificationChannel("broadcast", "sensor", NotificationManager.IMPORTANCE_HIGH);
                notificationManager.createNotificationChannel(mChannel);
                notification = new Notification.Builder(BroadcastActivity.this, null)
                        .setChannelId("broadcast")
                        .setContentTitle("动态广播")
                        .setContentText(intent.getStringExtra("one"))
                        .setSmallIcon(R.drawable.ic_launcher_background)
                        .setContentIntent(resultPendingIntent)
                        .build();
            } else {
                NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(BroadcastActivity.this, null)
                        .setContentTitle("动态广播")
                        .setContentText(intent.getStringExtra("one"))
                        .setSmallIcon(R.drawable.ic_launcher_background)
                        .setAutoCancel(true)
                        .setContentIntent(resultPendingIntent)
                        .setOngoing(true);
                try {
                    notification = notificationBuilder.build();
                } catch (Exception e) {
                    Log.e("TAG", "AndroidRuntime");
                }

            }
            notificationManager.notify(1, notification);
        }
    }
}